package com.quiz.source.database;


import java.util.ArrayList;
import java.util.List;

import com.quiz.source.model.details;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import android.widget.Toast;

@SuppressWarnings("unused")
public class QuizDbAdapter extends Activity{
	
	// Database fields
    public static final String KEY_ROWID = "_id";
    public static final String KEY_CATEGORY = "category";
    public static final String KEY_QUESTION = "question";
    public static final String KEY_OPTION1 = "option1";
    public static final String KEY_OPTION2 = "option2";
    public static final String KEY_OPTION3 = "option3";
    public static final String KEY_OPTION4 = "option4";
    public static final String KEY_ANSWER = "answer";
    private static final String DATABASE_TABLE = "quiz";
    private Context context;
    private SQLiteDatabase database;
    private QuizDatabaseHelper dbHelper;

    public QuizDbAdapter(Context context) {

        this.context = context;
    }

    //opens the database via the TodoDatabaseHelper class
    public QuizDbAdapter open() throws SQLException {

        dbHelper = new QuizDatabaseHelper(context);
        database = dbHelper.getWritableDatabase();
        return this;
    }

    public void close() {

        dbHelper.close();
    }

    /**
         * Create a new quiz If the quiz is successfully created return the new
         * rowId for that note, otherwise return a -1 to indicate failure.
         */

    public long createquiz(details item) {

        ContentValues initialValues = createContentValues(item);

        return database.insert(DATABASE_TABLE, null, initialValues);
    }

    /**
         * Update the quiz
         */

    public boolean updatequiz(long rowId, details item) {

        ContentValues updateValues = createContentValues(item);

        return database.update(DATABASE_TABLE, updateValues, KEY_ROWID + "=" + rowId, null) > 0;
    }

    /**
         * Deletes quiz
         */

    public boolean deletequiz(long rowId) {

        return database.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0;
    }

    /**
         * Return a Cursor over the list of all quiz in the database
         * 
         * @return Cursor over all notes
         */

    public Cursor fetchAllquiz() {

        return database.query(DATABASE_TABLE, new String[] {
                KEY_ROWID, KEY_CATEGORY, KEY_QUESTION, KEY_OPTION1, KEY_OPTION2, KEY_OPTION3, KEY_OPTION4, KEY_ANSWER
        }, null, null, null, null, null);
    }

    /**
         * Return a Cursor positioned at the defined quiz
         */

    public Cursor fetchquiz(long rowId) throws SQLException {

        Cursor mCursor = database.query(true, DATABASE_TABLE, new String[] {
                KEY_ROWID, KEY_CATEGORY, KEY_QUESTION, KEY_OPTION1, KEY_OPTION2, KEY_OPTION3, KEY_OPTION4, KEY_ANSWER
        }, KEY_ROWID + "=" + rowId, null, null, null, null, null);
        if (mCursor != null) {
            mCursor.moveToFirst();
        }
        return mCursor;
    }
    public Cursor fetchquiz_catogery(String str) throws SQLException {

        Cursor mCursor = database.query(true, DATABASE_TABLE, new String[] {
                KEY_ROWID, KEY_CATEGORY, KEY_QUESTION, KEY_OPTION1, KEY_OPTION2, KEY_OPTION3, KEY_OPTION4, KEY_ANSWER
        }, KEY_CATEGORY + "='" + str + "'", null, null, null, null, null);
        if (mCursor != null) {
            mCursor.moveToFirst();
        }
        return mCursor;
    }
    public Cursor fetchquiz_answer(String str) throws SQLException {

       //Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
    	Cursor mCursor = database.query(true, DATABASE_TABLE, new String[] {
                KEY_ROWID, KEY_CATEGORY, KEY_QUESTION, KEY_OPTION1, KEY_OPTION2, KEY_OPTION3, KEY_OPTION4, KEY_ANSWER
        }, KEY_ANSWER + "='" + str + "'", null, null, null, null, null);
        if (mCursor != null) {
            mCursor.moveToFirst();
        }
        return mCursor;
    }

    private ContentValues createContentValues(details items) {

        ContentValues values = new ContentValues();
        values.put(KEY_CATEGORY, items.category);
        values.put(KEY_QUESTION, items.question);
        values.put(KEY_OPTION1, items.option1);
        values.put(KEY_OPTION2, items.option2);
        values.put(KEY_OPTION3, items.option3);
        values.put(KEY_OPTION4, items.option4);
        values.put(KEY_ANSWER, items.answer);
        return values;
    }
    

}
